'use strict'

define(['jquery', 'bootstrap', 'select2', 'datetimepicker', 'datetimepickerCN',
        'shiftform', 'validate', 'validate.en'], function($) {
  
  return {
        
    init: function(data) {
    	this.initPage(data.roundCode, data.roundId, data.batchId, data.type, data.day);
        this.initCreate();
        this.initActionGroup();
        this.initDelete();
        this.initLink();
        this.initEditDiet();
        this.save(data.roundCode, data.roundId, data.batchId, data.type);
        this.initEditTags();
    },
    
    initPage: function(roundCode, roundId, batchId, type, day) {
        var that = this;
        
        if(day) {
            var scrollDistance = $(".schedule-action-layout .action-module").eq(day-1).position().top
            $(".schedule-action-layout").scrollTop(scrollDistance -10);
        }
    },
    
    initCreate: function(){
        var that = this;
        
        $(document).on("shown.bs.modal","#create, #edit", function(e) {
            var $form = $(e.currentTarget).find("form");
            
              $form.bt_validate();
              
              $form.shiftform({
                beforeSubmit : function(el) {
                    if($('input[name="time"]').val() && isNaN($('input[name="time"]').val())) {
                        return false;
                    }
                    var $groupAmount = $('input[name="groupAmount"]');
                    var $movementNumber = $('input[name="movementNumber"]');
                    if($groupAmount.val() && isNaN($groupAmount.val())){
                        return false;
                    }
                    if(!$movementNumber.val()){
                        $movementNumber.val(0);
                    }
                    if(isNaN($movementNumber.val())) {
                        return false;
                    }
                    
                  $('form').trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              });
        })
             
    },
    
    initActionGroup: function(){
        var that = this;
        
        $(document).on("shown.bs.modal","#action-group", function(e) {
            var $form = $(e.currentTarget).find("form");
            
              $form.bt_validate();
              
              $form.shiftform({
                beforeSubmit : function(el) {
                  $('form').trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              });
        })
             
    },
 
      initDelete: function(){
          $(document).on("shown.bs.modal","#rest, #delete, #save-all, #offline-link", function(e) {
               var $form = $(e.currentTarget).find("form");
               $form.shiftform();
          })
               
      },
      
      initLink: function(){
          $(document).on("shown.bs.modal","#link", function(e) {
        	  var $form = $(e.currentTarget).find("form");
              
              $form.bt_validate();
              
              $form.shiftform({
                beforeSubmit : function(el) {
                  $('form').trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              });
          })
               
      },
      
      initEditDiet: function(){
          var that = this;
          
          $(document).on("shown.bs.modal","#diet", function(e) {
              var $form = $(e.currentTarget).find("form");
              
                $form.bt_validate();
                
                $form.shiftform({
                  beforeSubmit : function(el) {
                    $('form').trigger("validate.ajax.submit");
                    return $.bt_validate.result[$.bt_validate.form_id]
                  }
                });
          })
               
      },
      
      save: function(roundCode, roundId, batchId, type){
          
          $(document).on("click",".btn-save", function(e) {
        	  var url;
        	  if(type == 'online') {
        		  url = '/online-rounds/' + roundCode + '/rounds/' + roundId;
        	  } else {
        		  url = '/individual-rounds/' + roundId;
        	  }
        	  
              $.ajax({
                  type: 'POST',
                  url: window.__CTX__.root + url + '/batch-coursewares/' + batchId,
                  data: {
                	   'breakfastDescription': $('textarea[name="breakfastDescription"]').val(),
                	   'lunchDescription': $('textarea[name="lunchDescription"]').val(),
                	   'dinnerDescription': $('textarea[name="dinnerDescription"]').val(),
                	   'breakfastSnacksDescription': $('textarea[name="breakfastSnacksDescription"]').val(),
                	   'lunchSnacksDescription': $('textarea[name="lunchSnacksDescription"]').val(),
                	   'dinnerSnacksDescription': $('textarea[name="dinnerSnacksDescription"]').val(),
                	   '_method': 'PATCH'
                  },
                  cache:false
                }).done(function(data) {
                    
                    var toastHtml = "<div class='toast-section'>"+
                                       "<p class='toast-text'>保存成功！</p>"+
                                     "</div>"
                    $("body").append(toastHtml);
                    setTimeout(' $(".toast-section").remove();',1500)
                }).fail(function() {
                    var toastHtml = "<div class='toast-section'>"+
                                       "<p class='toast-text'>保存失败！</p>"+
                                     "</div>"
                    $("body").append(toastHtml)
                    setTimeout(' $(".toast-section").remove();',1500)
                })
         })
      },
      
      initEditTags: function(){
          var that = this;
          
          $(document).on("shown.bs.modal","#edit-tags", function(e) {
              var $form = $(e.currentTarget).find("form");
              
                $form.bt_validate();
                
                $form.shiftform({
                  beforeSubmit : function(el) {
                    $('form').trigger("validate.ajax.submit");
                    return $.bt_validate.result[$.bt_validate.form_id]
                  }
                });
          })
               
      },
        
      
   }
})
